package com.hardydou.data.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.data.model.Film;
import com.hardydou.data.service.IFilmService;
import com.hardydou.framework.log.anno.LogMetaInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020/7/15
 * @Copyright ：Hardydou.com
 */
@Controller
@LogMetaInfo(module = "影视API")
@RequestMapping(ApiFilmController.REQ_PREFIX)
public class ApiFilmController extends BaseController {
    final static String REQ_PREFIX = "/api/data/film";
    @Autowired
    IFilmService filmService;

    //-----------JSON API---------//


    @ApiOperation(value = "影视列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listData(int pageNum, int pageSize, @RequestParam Map params) {
        List<Film> films = filmService.list(params);
        PageInfo<Film> pageInfo = new PageInfo<>(films);
        return ResultHelper.ok(pageInfo);
    }

    @ApiOperation(value = "首页资源")
    @GetMapping("/index.json")
    @ResponseBody
    public Result<?> homeData(String filmTypes) {
        Map data = new HashMap();
        int count = 6;
        data.put("latest", filmService.findLatest(count));
        data.put("favorite", filmService.findByType(filmTypes, count));
        data.put("hot", filmService.findHots(count));
        return ResultHelper.ok(data);
    }

    @ApiOperation(value = "影视搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/search.json")
    @ResponseBody
    public Result<?> searchData(int pageNum, int pageSize, String keywords) {
        //TODO 全文索引
        Map params = new HashMap();
        params.put("name", keywords);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        List<Film> films = filmService.list(params);
        PageInfo<Film> pageInfo = new PageInfo<>(films);
        return ResultHelper.ok(pageInfo);
    }

    @ApiOperation(value = "影视详情")
    @GetMapping("/detail.json")
    @ResponseBody
    public Result<?> detail(Long resId) {
        Film film = filmService.findById(resId);
        ResultAssert.notNull(film);
        film.setInfo(filmService.findInfoById(resId));
        return ResultHelper.ok(film);
    }

    /***
     * 主模块名称
     * @return
     */
    @Override
    public String getModel() {
        return "data";
    }

    /***
     * 子模块名称
     * @see #page(String)
     * @return
     */
    @Override
    public String getSubModel() {
        return "film";
    }
}
